#A first example

class LoggingContextManager:
    def __enter__(self):
        return self
    def __exit__(self, exc_type, exc_val, exc_tb):
        return

with LoggingContextManager() as x:
    print(x)


class LoggingContextManager:
    def __enter__(self):
        return "You're in a with-block!"
    def __exit__(self, exc_type, exc_val, exc_tb):
        return

with LoggingContextManager() as x:
    print(x)


class LoggingContextManager:
    def __enter__(self):
        print('LoggingContextManager.__enter__()')
        return "You're in a with-block!"
    def __exit__(self, exc_type, exc_val, exc_tb):
        print('LoggingContextManager.__exit__({}, {}, {})'.format(exc_type, exc_val, exc_tb))
        return

with LoggingContextManager() as x:
    print(x)

with LoggingContextManager() as x:
    raise ValueError("Something has gone wrong!")
    print(x)
 

#__enter__()
f = open('a_file', 'w')
with f as g:
    print(f is g)

#__exit__()
class LoggingContextManager:
    def __enter__(self):
        print('LoggingContextManager.__enter__()')
        return "You're in a with-block!"
    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type is None:
            print('LoggingContextManager.__exit__: normal exit detected')
        else:
            print('LoggingContextManager.__exit__: Exception detected!'
                  ' type={}, value={}, traceback={}'.format(exc_type, exc_val, exc_tb))


with LoggingContextManager():
    pass

with LoggingContextManager():
    raise ValueError('Core meltdown imminent!')


try:
    with LoggingContextManager():
        raise ValueError('The system is down!')
except ValueError:
    print('*** ValueError detected ***')